<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// Inclure les fichiers nécessaires de PHPMailer
require 'phpmailer/Exception.php';
require 'phpmailer/PHPMailer.php';
require 'phpmailer/SMTP.php';

// Capturer les données du formulaire POST
$nom = $_POST['name'] ?? '';
$email = $_POST['email'] ?? '';
$sujet = $_POST['subject'] ?? '';
$message = $_POST['message'] ?? '';

// Configuration SMTP
$smtpHost = 'smtp.ionos.fr';
$smtpUsername = 'info@stock-exchange-crypto.com';
$smtpPassword = 'Dogsecurite.1982';
$smtpPort = 465;
$smtpSecure = PHPMailer::ENCRYPTION_SMTPS; // Utilisation de ENCRYPTION_SMTPS pour le port 465
$fromEmail = 'info@stock-exchange-crypto.com';
$fromName = 'Stock Echange Crypto';
$recipient = 'globalservice@orange.fr';

// Construire le corps du message HTML
$messageBody = "
<html>
<head>
    <style>
        .email-container {
            font-family: Arial, sans-serif;
            color: #333333;
            line-height: 1.6;
        }
        .email-header {
            background-color: #08553C;
            color: #FFFFFF;
            padding: 20px;
            text-align: center;
        }
        .email-body {
            padding: 20px;
        }
        .email-footer {
            background-color: #08553C;
            color: #FFFFFF;
            padding: 10px;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class='email-container'>
        <div class='email-header'>
            <h1>Nouveau message de contact</h1>
        </div>
        <div class='email-body'>
            <p><strong>Nom:</strong> $nom</p>
            <p><strong>Email:</strong> $email</p>
            <p><strong>Sujet:</strong> $sujet</p>
            <p><strong>Message:</strong></p>
            <p>$message</p>
        </div>
        <div class='email-footer'>
            <p>&copy; " . date('Y') . " Stock Exchange Crypto. Tous droits réservés.</p>
        </div>
    </div>
</body>
</html>
";

// Créer une nouvelle instance de PHPMailer
$mail = new PHPMailer(true);

try {
    // Configuration du serveur SMTP
    $mail->isSMTP();
    $mail->Host = $smtpHost;
    $mail->SMTPAuth = true;
    $mail->Username = $smtpUsername;
    $mail->Password = $smtpPassword;
    $mail->SMTPSecure = $smtpSecure;
    $mail->Port = $smtpPort;

    // Définir l'encodage des caractères
    $mail->CharSet = 'UTF-8';

    // Destinataire et sujet
    $mail->setFrom($fromEmail, $fromName);
    $mail->addAddress($recipient); // Adresse du destinataire
    $mail->addBCC('info@saint-jean-cap-ferrat-network.fr'); // Adresse en copie cachée si nécessaire
    $mail->Subject = 'Nouveau message de contact'; // Sujet fixe pour l'email envoyé à l'équipe

    // Contenu de l'e-mail
    $mail->isHTML(true);
    $mail->Body = $messageBody;

    // Envoi de l'e-mail
    if ($mail->send()) {
        // Message de succès si l'e-mail est envoyé avec succès
        echo '<html><body>';
        echo '<div style="text-align: center; background-color: #f0f0f0; padding: 20px; margin: auto; width: 50%;">';
        echo '<h2 style="color: #08553C;">Merci ! Votre message a bien été envoyé.</h2>';
        echo '<p>Voici un récapitulatif de votre message :</p>';
        echo '<ul style="list-style-type: none; padding-left: 0; text-align: left;">';
        echo '<li><strong>Nom:</strong> ' . htmlspecialchars($nom) . '</li>';
        echo '<li><strong>Email:</strong> ' . htmlspecialchars($email) . '</li>';
        echo '<li><strong>Sujet:</strong> ' . htmlspecialchars($sujet) . '</li>';
        echo '<li><strong>Message:</strong><br>' . nl2br(htmlspecialchars($message)) . '</li>';
        echo '</ul>';
        echo '</div>';
        echo '</body></html>';
    } else {
        // Message d'erreur si l'e-mail n'a pas pu être envoyé
        echo '<html><body>';
        echo '<div style="text-align: center; background-color: #f0f0f0; padding: 20px; margin: auto; width: 50%; color: #d8000c;">';
        echo '<h2>Une erreur est survenue. Votre message n\'a pas pu être envoyé.</h2>';
        echo '<p>Veuillez réessayer plus tard ou nous contacter directement.</p>';
        echo '</div>';
        echo '</body></html>';
    }
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>